import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:laos_mall/app_route.dart';
import 'package:laos_mall/common/base/page_size_controller.dart';
import 'package:laos_mall/common/utils/http_util.dart';
import 'package:laos_mall/common/utils/widget_util.dart';
import 'package:laos_mall/common/widght/toast/showtoast.dart';
import 'package:laos_mall/models/cart/cart_model.dart';
import 'package:laos_mall/user_data.dart';

class ShoppingCartLogic extends PageSizeLogic<CartItemModel> with GetTickerProviderStateMixin{
  late TabController tabController;
  var isManager = false.obs;
  var showBottomBox = true.obs;
  // var dataSource = List.generate(10, (i) => {"name":'item$i',"state":false}).obs;
  var isAll = false.obs;
  bool allState = true;

  CartModel? cartModel;
  @override
  Future<List<CartItemModel>> Function()? get future => ()async{
    var req = await HttpUtil.restApi.cartList({});
    List<CartItemModel>? items = [];
    req.data?.cart_list?.forEach((element){
      items.addAll(element.carts ?? []);
    } );
    cartModel = req.data;
    isAll.value =  cartModel?.total?.checked_count == cartModel?.total?.total_count ? true :false;
    allState = !isAll.value;
    UserData().cartNum.value = cartModel?.total?.total_count ?? 0;
    return items;
  };

  @override
  void onInit() {
    tabController = TabController(length: 2, vsync: this);
    tabController.addListener(() {
      showBottomBox.value = tabController.index == 0 ? true : false;
    });
    super.onInit();
  }


  clickContinue(){
    if(!UserData().loginJudge()){
      return;
    }
    if(getNumberSel() == 0){
      showToast("hint_cart_continue".tr);
      return;
    }
    Get.toNamed(AppRouter.checkout);
  }


  clickSelectIcon(index)async{
    if(!UserData().loginJudge()){
      return;
    }
    dataSource[index].is_checked = !(dataSource[index].is_checked ?? false);
    var e = dataSource[index];
    var param = {"data":[{"cart_id":e.cart_id,"is_checked":e.is_checked}]};
    await HttpUtil.restApi.updateCheck(param);
    update();
    onRefresh();
  }

  clickAll()async{
    if(!UserData().loginJudge()){
      return;
    }
    isAll.value = !isAll.value;
    dataSource.forEach((element) {
      element.is_checked =  allState;
    });
    var result = dataSource.map((e) => {"cart_id":e.cart_id,"is_checked":allState}).toList();
    var param = {"data":result};
    print(param);
    await HttpUtil.restApi.updateCheck(param);
    allState = !allState;
    onRefresh();
  }

  getNumberSel(){
    // var data = dataSource.where((p0) =>  p0.is_checked == true).toList();
    // return data.length;
    return cartModel?.total?.checked_count ?? 0;
  }

  cancelCheck(){
    if(!UserData().loginJudge()){
      return;
    }
    ///todo: 取消勾选 不是取消删除的意思
    allState = false;
    isAll.value = false;
   clickAll();
  }

  cleanAll(){
    if(!UserData().loginJudge()){
      return;
    }
    WidgetUtil.showBottomSheet("hint_clear_all".tr, "", ()async {
      await HttpUtil.restApi.cleanCart({});
      onRefresh();
    },okText: "delete".tr);
  }

  deleteProduct(index){
    WidgetUtil.showBottomSheet("hint_delete".tr, "", ()async {
      var item = dataSource[index];
      await HttpUtil.restApi.deleteCart({"cart_ids":[item.cart_id]});
      onRefresh();
    },okText: "delete".tr);
  }

  @override
  void dispose() {
    tabController.dispose();
    super.dispose();
  }
}
